{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import plotly\n",
    "import plotly.graph_objs as go\n",
    "plotly.offline.init_notebook_mode(connected=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Pie Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "labels": [
          "Oxygen",
          "Hydrogen",
          "Carbon_Dioxide",
          "Nitrogen"
         ],
         "type": "pie",
         "uid": "2e574503-a57d-4ea2-aa41-b770f8461ff6",
         "values": [
          4500,
          2500,
          1053,
          500
         ]
        }
       ],
       "layout": {}
      },
      "text/html": [
       "<div id=\"fc57a114-38da-42ff-b7fc-bab6594e18ba\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"fc57a114-38da-42ff-b7fc-bab6594e18ba\", [{\"labels\": [\"Oxygen\", \"Hydrogen\", \"Carbon_Dioxide\", \"Nitrogen\"], \"values\": [4500, 2500, 1053, 500], \"type\": \"pie\", \"uid\": \"18aaba2e-5667-4f5f-9d10-a7d1e3578a3e\"}], {}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"fc57a114-38da-42ff-b7fc-bab6594e18ba\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"fc57a114-38da-42ff-b7fc-bab6594e18ba\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"fc57a114-38da-42ff-b7fc-bab6594e18ba\", [{\"labels\": [\"Oxygen\", \"Hydrogen\", \"Carbon_Dioxide\", \"Nitrogen\"], \"values\": [4500, 2500, 1053, 500], \"type\": \"pie\", \"uid\": \"18aaba2e-5667-4f5f-9d10-a7d1e3578a3e\"}], {}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"fc57a114-38da-42ff-b7fc-bab6594e18ba\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']\n",
    "values = [4500,2500,1053,500]\n",
    "\n",
    "trace = go.Pie(labels=labels, values=values)\n",
    "plotly.offline.iplot([trace], filename='basic_pie_chart')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Styled Pie Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "hoverinfo": "label+percent",
         "labels": [
          "Oxygen",
          "Hydrogen",
          "Carbon_Dioxide",
          "Nitrogen"
         ],
         "marker": {
          "colors": [
           "#FEBFB3",
           "#E1396C",
           "#96D38C",
           "#D0F9B1"
          ],
          "line": {
           "color": "#000000",
           "width": 2
          }
         },
         "textfont": {
          "size": 20
         },
         "textinfo": "value",
         "type": "pie",
         "uid": "595b33b4-b572-44f5-9a18-99eeea5b0286",
         "values": [
          4500,
          2500,
          1053,
          500
         ]
        }
       ],
       "layout": {}
      },
      "text/html": [
       "<div id=\"74645c11-3167-49ac-bcf3-3f234588d068\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"74645c11-3167-49ac-bcf3-3f234588d068\", [{\"hoverinfo\": \"label+percent\", \"labels\": [\"Oxygen\", \"Hydrogen\", \"Carbon_Dioxide\", \"Nitrogen\"], \"marker\": {\"colors\": [\"#FEBFB3\", \"#E1396C\", \"#96D38C\", \"#D0F9B1\"], \"line\": {\"color\": \"#000000\", \"width\": 2}}, \"textfont\": {\"size\": 20}, \"textinfo\": \"value\", \"values\": [4500, 2500, 1053, 500], \"type\": \"pie\", \"uid\": \"4702e940-ba85-43ca-8e5b-86ad1a94f830\"}], {}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"74645c11-3167-49ac-bcf3-3f234588d068\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"74645c11-3167-49ac-bcf3-3f234588d068\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"74645c11-3167-49ac-bcf3-3f234588d068\", [{\"hoverinfo\": \"label+percent\", \"labels\": [\"Oxygen\", \"Hydrogen\", \"Carbon_Dioxide\", \"Nitrogen\"], \"marker\": {\"colors\": [\"#FEBFB3\", \"#E1396C\", \"#96D38C\", \"#D0F9B1\"], \"line\": {\"color\": \"#000000\", \"width\": 2}}, \"textfont\": {\"size\": 20}, \"textinfo\": \"value\", \"values\": [4500, 2500, 1053, 500], \"type\": \"pie\", \"uid\": \"4702e940-ba85-43ca-8e5b-86ad1a94f830\"}], {}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"74645c11-3167-49ac-bcf3-3f234588d068\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']\n",
    "values = [4500,2500,1053,500]\n",
    "colors = ['#FEBFB3', '#E1396C', '#96D38C', '#D0F9B1']\n",
    "\n",
    "trace = go.Pie(\n",
    "    labels=labels, values=values,\n",
    "    hoverinfo='label+percent', \n",
    "    textinfo='value', \n",
    "    textfont=dict(size=20),\n",
    "    marker=dict(\n",
    "        colors=colors, \n",
    "        line=dict(color='#000000', width=2)\n",
    "    )\n",
    ")\n",
    "plotly.offline.iplot([trace], filename='styled_pie_chart')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Donut Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "domain": {
          "x": [
           0,
           0.48
          ],
          "y": [
           0,
           1
          ]
         },
         "hole": 0.4,
         "hoverinfo": "label+percent+name",
         "labels": [
          "US",
          "China",
          "European Union",
          "Russian Federation",
          "Brazil",
          "India",
          "Rest of World"
         ],
         "name": "GHG Emissions",
         "type": "pie",
         "uid": "32fa5d30-977f-4fa3-bec9-30b7cdd52d19",
         "values": [
          16,
          15,
          12,
          6,
          5,
          4,
          42
         ]
        },
        {
         "domain": {
          "x": [
           0.52,
           1
          ],
          "y": [
           0,
           1
          ]
         },
         "hole": 0.4,
         "hoverinfo": "label+percent+name",
         "labels": [
          "US",
          "China",
          "European Union",
          "Russian Federation",
          "Brazil",
          "India",
          "Rest of World"
         ],
         "name": "CO2 Emissions",
         "text": [
          "CO2",
          "CO2",
          "CO2",
          "CO2",
          "CO2",
          "CO2",
          "CO2"
         ],
         "textposition": "inside",
         "type": "pie",
         "uid": "e7b3bdb8-1c4e-4aec-a46b-e836aa8747cc",
         "values": [
          27,
          11,
          25,
          8,
          1,
          3,
          25
         ]
        }
       ],
       "layout": {
        "annotations": [
         {
          "font": {
           "size": 20
          },
          "showarrow": false,
          "text": "GHG",
          "x": 0.2,
          "y": 0.5
         },
         {
          "font": {
           "size": 20
          },
          "showarrow": false,
          "text": "CO2",
          "x": 0.8,
          "y": 0.5
         }
        ],
        "title": "Global Emissions 1990-2011"
       }
      },
      "text/html": [
       "<div id=\"24076b94-e5d5-4436-b2f6-3c0dc001e9f7\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"24076b94-e5d5-4436-b2f6-3c0dc001e9f7\", [{\"domain\": {\"x\": [0, 0.48], \"y\": [0, 1]}, \"hole\": 0.4, \"hoverinfo\": \"label+percent+name\", \"labels\": [\"US\", \"China\", \"European Union\", \"Russian Federation\", \"Brazil\", \"India\", \"Rest of World\"], \"name\": \"GHG Emissions\", \"values\": [16, 15, 12, 6, 5, 4, 42], \"type\": \"pie\", \"uid\": \"4353a1f2-5fd3-4d44-9ff3-d2d95a5c34cb\"}, {\"domain\": {\"x\": [0.52, 1], \"y\": [0, 1]}, \"hole\": 0.4, \"hoverinfo\": \"label+percent+name\", \"labels\": [\"US\", \"China\", \"European Union\", \"Russian Federation\", \"Brazil\", \"India\", \"Rest of World\"], \"name\": \"CO2 Emissions\", \"text\": [\"CO2\", \"CO2\", \"CO2\", \"CO2\", \"CO2\", \"CO2\", \"CO2\"], \"textposition\": \"inside\", \"values\": [27, 11, 25, 8, 1, 3, 25], \"type\": \"pie\", \"uid\": \"e7c12093-bc87-4e4c-9464-c984699a098c\"}], {\"annotations\": [{\"font\": {\"size\": 20}, \"showarrow\": false, \"text\": \"GHG\", \"x\": 0.2, \"y\": 0.5}, {\"font\": {\"size\": 20}, \"showarrow\": false, \"text\": \"CO2\", \"x\": 0.8, \"y\": 0.5}], \"title\": \"Global Emissions 1990-2011\"}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"24076b94-e5d5-4436-b2f6-3c0dc001e9f7\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"24076b94-e5d5-4436-b2f6-3c0dc001e9f7\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"24076b94-e5d5-4436-b2f6-3c0dc001e9f7\", [{\"domain\": {\"x\": [0, 0.48], \"y\": [0, 1]}, \"hole\": 0.4, \"hoverinfo\": \"label+percent+name\", \"labels\": [\"US\", \"China\", \"European Union\", \"Russian Federation\", \"Brazil\", \"India\", \"Rest of World\"], \"name\": \"GHG Emissions\", \"values\": [16, 15, 12, 6, 5, 4, 42], \"type\": \"pie\", \"uid\": \"4353a1f2-5fd3-4d44-9ff3-d2d95a5c34cb\"}, {\"domain\": {\"x\": [0.52, 1], \"y\": [0, 1]}, \"hole\": 0.4, \"hoverinfo\": \"label+percent+name\", \"labels\": [\"US\", \"China\", \"European Union\", \"Russian Federation\", \"Brazil\", \"India\", \"Rest of World\"], \"name\": \"CO2 Emissions\", \"text\": [\"CO2\", \"CO2\", \"CO2\", \"CO2\", \"CO2\", \"CO2\", \"CO2\"], \"textposition\": \"inside\", \"values\": [27, 11, 25, 8, 1, 3, 25], \"type\": \"pie\", \"uid\": \"e7c12093-bc87-4e4c-9464-c984699a098c\"}], {\"annotations\": [{\"font\": {\"size\": 20}, \"showarrow\": false, \"text\": \"GHG\", \"x\": 0.2, \"y\": 0.5}, {\"font\": {\"size\": 20}, \"showarrow\": false, \"text\": \"CO2\", \"x\": 0.8, \"y\": 0.5}], \"title\": \"Global Emissions 1990-2011\"}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"24076b94-e5d5-4436-b2f6-3c0dc001e9f7\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 使用字典形式\n",
    "fig = {\n",
    "    \"data\": [\n",
    "        {\n",
    "            \"values\": [16, 15, 12, 6, 5, 4, 42],\n",
    "            \"labels\": [\"US\", \"China\", \"European Union\", \"Russian Federation\", \"Brazil\", \"India\", \"Rest of World\"],\n",
    "            # 设置显示区域占左半部分\n",
    "            \"domain\": {\n",
    "                \"x\": [0, .48],\n",
    "                \"y\": [0, 1],\n",
    "            },\n",
    "            \"name\": \"GHG Emissions\",\n",
    "            \"hoverinfo\":\"label+percent+name\",\n",
    "            # 设置空心的大小\n",
    "            \"hole\": .4,\n",
    "            \"type\": \"pie\"\n",
    "        },\n",
    "        {\n",
    "            \"values\": [27, 11, 25, 8, 1, 3, 25],\n",
    "            \"labels\": [\"US\", \"China\", \"European Union\", \"Russian Federation\", \"Brazil\", \"India\", \"Rest of World\"],\n",
    "            \"text\":[\"CO2\"] * 7,\n",
    "            \"textposition\":\"inside\",\n",
    "            # 设置显示区域占右半部分\n",
    "            \"domain\": {\n",
    "                \"x\": [.52, 1],\n",
    "                \"y\": [0, 1],\n",
    "            },\n",
    "            \"name\": \"CO2 Emissions\",\n",
    "            \"hoverinfo\":\"label+percent+name\",\n",
    "            # 设置空心的大小\n",
    "            \"hole\": .4,\n",
    "            \"type\": \"pie\"\n",
    "        }\n",
    "    ],\n",
    "    \"layout\": {\n",
    "        \"title\":\"Global Emissions 1990-2011\",\n",
    "        \"annotations\": [\n",
    "            {\n",
    "                \"font\": {\"size\": 20},\n",
    "                \"showarrow\": False,\n",
    "                \"text\": \"GHG\",\n",
    "                \"x\": 0.20,\n",
    "                \"y\": 0.5\n",
    "            },\n",
    "            {\n",
    "                \"font\": {\"size\": 20},\n",
    "                \"showarrow\": False,\n",
    "                \"text\": \"CO2\",\n",
    "                \"x\": 0.8,\n",
    "                \"y\": 0.5\n",
    "            }\n",
    "        ]\n",
    "    }\n",
    "}\n",
    "plotly.offline.iplot(fig, filename='donut')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
